{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f2330362",
   "metadata": {},
   "source": [
    "# 1.串行计算圆周率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "802ca43b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "def caculate_pi(num):\n",
    "    points = 0\n",
    "    for _ in range(num):\n",
    "        x = np.random.uniform(0, 1)\n",
    "        y = np.random.uniform(0, 1)\n",
    "\n",
    "        if x**2+y**2 <=1:\n",
    "            points += 1  #计算在圆的点\n",
    "\n",
    "    pi_estimate = (4 * points)/num\n",
    "    return pi_estimat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63d66f3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "num = 1000000\n",
    "pi = caculate_pi(num)\n",
    "print(f\"估算的圆周率: {pi}\")    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5efa15c0",
   "metadata": {},
   "source": [
    "# 2.并行计算圆周率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9bcb37d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import dask\n",
    "import numpy as np\n",
    "@dask.delayed\n",
    "def point(num):\n",
    "    points_1 = 0\n",
    "    for i in range(num):\n",
    "        a = np.random.uniform(0, 1)\n",
    "        b = np.random.uniform(0, 1)\n",
    "\n",
    "        if a**2+b**2 <=1:\n",
    "            points_1 += 1\n",
    "    return points_1\n",
    "\n",
    "@dask.delayed\n",
    "def count(pt):\n",
    "    return sum(pt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d43313e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dask.distributed import Client\n",
    "\n",
    "client = Client(n_workers=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cffbf5af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "估算的圆周率:3.141578768\n",
      "CPU times: total: 7min 33s\n",
      "Wall time: 52min 49s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "n=10000\n",
    "num=1000000000\n",
    "pt=[point(num//n) for i in range(n)]\n",
    "total = count(pt)\n",
    "pi=total/num\n",
    "result=pi.compute()\n",
    "print(f\"估算的圆周率:{result*4}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7522cd93",
   "metadata": {},
   "outputs": [],
   "source": [
    "client.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a55c35f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
